package io.finer.erp.jeecg.stock.service.impl;

import io.finer.erp.jeecg.stock.entity.StkTray;
import io.finer.erp.jeecg.stock.entity.StkTrayDetail;
import io.finer.erp.jeecg.stock.mapper.StkTrayDetailMapper;
import io.finer.erp.jeecg.stock.mapper.StkTrayMapper;
import io.finer.erp.jeecg.stock.service.IStkTrayService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.util.FillRuleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 发货托盘表
 * @Author: jeecg-boot
 * @Date:   2020-10-20
 * @Version: V1.0
 */
@Service
public class StkTrayServiceImpl extends ServiceImpl<StkTrayMapper, StkTray> implements IStkTrayService {

	@Autowired
	private StkTrayMapper stkTrayMapper;
	@Autowired
	private StkTrayDetailMapper stkTrayDetailMapper;
	@Autowired
	private ISysBaseAPI baseApi;
	
	@Override
	@Transactional
	public void saveMain(StkTray stkTray, List<StkTrayDetail> stkTrayDetailList) {
		stkTrayMapper.insert(stkTray);
		if(stkTrayDetailList!=null && stkTrayDetailList.size()>0) {
			for(StkTrayDetail entity:stkTrayDetailList) {
				//外键设置
				entity.setTrayId(stkTray.getId());
				stkTrayDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(StkTray stkTray,List<StkTrayDetail> stkTrayDetailList) {
		stkTrayMapper.updateById(stkTray);
		
		//1.先删除子表数据
		stkTrayDetailMapper.deleteByMainId(stkTray.getId());
		
		//2.子表数据重新插入
		if(stkTrayDetailList!=null && stkTrayDetailList.size()>0) {
			for(StkTrayDetail entity:stkTrayDetailList) {
				//外键设置
				entity.setTrayId(stkTray.getId());
				stkTrayDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		stkTrayDetailMapper.deleteByMainId(id);
		stkTrayMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			stkTrayDetailMapper.deleteByMainId(id.toString());
			stkTrayMapper.deleteById(id);
		}
	}

	@Override
	public String createTrayCode() {
		String code = baseApi.getNoByCode("stk_tray_no");
		return code;
	}

	@Override
	public List<StkTray> getTrayByCodes(List<String> trayCodes) {
		if(trayCodes == null || trayCodes.size() == 0) {
			return null;
		}
		LambdaQueryWrapper<StkTray> query = new LambdaQueryWrapper<>();
		query.in(StkTray::getCode, trayCodes);
		return list(query);
	}

	@Override
	public StkTray getTrayByCode(String code) {
		if(StringUtils.isBlank(code)) {
			return null;
		}
		LambdaQueryWrapper<StkTray> query = new LambdaQueryWrapper<>();
		query.eq(StkTray::getCode, code);
		return getOne(query);
	}
	
}
